package com.ctrip.cap.client.impl.base;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctrip.cap.client.property.CapClientProperties;
import com.ctrip.cap.common.util.UrlUtil;
import com.ctrip.cap.exception.CapDataException;

public class BaseClientImpl extends BaseClient{
	private static final Logger logger = LoggerFactory
			.getLogger(BaseClientImpl.class);

	@Override
	protected <T> T baseCall(String path,  Object param, HttpMethodType methodType, Class<T> resultClaz, String queryParamName) {
		
		
		Client client = ClientBuilder.newClient();
		WebTarget webTarget = client
				.target(CapClientProperties.capDataUrl)
				.property(ClientProperties.CONNECT_TIMEOUT,
						CapClientProperties.TIMEOUT_MS)
						.register(JacksonFeature.class);	
		
		if(path!=null)
			webTarget = webTarget.path(path);
		
		
		Response response = null;
		
		switch(methodType==null?0:methodType.getVal())
		{
		case 0:
			response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(param, MediaType.APPLICATION_JSON));
			break;
		case 1:
			response = webTarget.queryParam(queryParamName, UrlUtil.encodeURL(param)).request().get();
			break;
		case 2:
			response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).put(Entity.entity(param, MediaType.APPLICATION_JSON));
			break;
		case 3:
			response = webTarget.queryParam(queryParamName, UrlUtil.encodeURL(param)).request().delete();
			break;
		default:
			response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(param, MediaType.APPLICATION_JSON));
			break;
		}

		if (response.getStatus() != 200) {
			throw new CapDataException(response.readEntity(String.class));
		}else{
			logger.debug("HTTP Response Status is 200.");
		}
		
		
		if(resultClaz == null)
			return null;
		else
			return response.readEntity(resultClaz);
		
	}
	
	public String helloWorld1() {

		return baseCall("/service/var2", "xiaoming", HttpMethodType.GET);
	}

	public String helloWorld2() {

		return baseCall("/service/var2", "xiaoming", HttpMethodType.GET, String.class, "param");
	}
	
	public String helloWorld3() {

		return baseCall("/service/var3", "xiaoming", HttpMethodType.POST, String.class);
	}
	
	public static enum HttpMethodType {
		POST(0), GET(1), PUT(2), DELETE(3);

		private int val;

		private HttpMethodType(int val) {
			this.val = val;
		}

		public int getVal() {
			return val;
		}

	}
	
	
	public static void main(String args[]){
		/*BaseClientImpl client = new BaseClientImpl();
		System.out.println(client.helloWorld1());
		System.out.println(client.helloWorld2());
		System.out.println(client.helloWorld3());*/
		String path = "/api/json";
		Client client = ClientBuilder.newClient();
		WebTarget webTarget = client
				.target("http://cimobile.dev.sh.ctripcorp.com:8080")
				.property(ClientProperties.CONNECT_TIMEOUT,
						CapClientProperties.TIMEOUT_MS).register(JacksonFeature.class);
		
		Response response = webTarget.path(path).queryParam("pretty", true).request(MediaType.APPLICATION_JSON).get();
		System.out.println(response.readEntity(String.class));
	}
	
	
}
